package com.example.jegotrip.utils;

import static com.google.android.gms.common.util.ClientLibraryUtils.getPackageInfo;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;

import java.util.ArrayList;
import java.util.List;

public class AppData {
    private AppData() {}

    public static AppData getInstance() {
        return AppDataHolder.instance;
    }

    private static class AppDataHolder {
        private static final AppData instance = new AppData();
    }

    public void getAppInfo(Context context) {
        PackageManager packageManager = context.getPackageManager(); // 获取PackageManager实例

        try {
            // 使用packageName来获取应用的信息
            ApplicationInfo appInfo = packageManager.getApplicationInfo("com.example.jegotrip", 0);
            // 如果成功获取到应用信息，继续下面的步骤
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace(); // 捕获异常
        }
    }

    /**
     * 获取到系统的app功能:    // 获取所有已安装应用的 PackageInfo 列表
     * @param context
     * @return
     */
    public List<ApplicationInfo> getAllApps(Context context) {
        List<ApplicationInfo> apps = new ArrayList<>();
        PackageManager packageManager = context.getPackageManager();
        // GET_META_DATA表示获取应用的元数据
        List<ApplicationInfo> installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo appInfo : installedApps) {
            // 通过appInfo.packageName可以获取应用的包名

            // 过滤掉系统应用和非启动器的第三方应用
            if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                // 非系统应用
                // apps.add(appInfo);
            } else if (isLauncherApp(packageManager, appInfo)) {
                // 系统应用且是启动器应用
//                LogUtils.d("appInfo.packageName==：" +appInfo.packageName);
                apps.add(appInfo);
            }
        }
        return apps;
    }

    private boolean isLauncherApp(PackageManager packageManager, ApplicationInfo applicationInfo) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setPackage(applicationInfo.packageName);

        List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);
        return !resolveInfoList.isEmpty();
    }

    /**
     * 判断应用是否已安装:通过包名调用 getPackageInfo() 方法，如果返回非空，则说明应用已安装。
     * @param context
     * @param packageName
     * @return
     */
    public boolean isAppInstalled(Context context, String packageName) {
        if (packageName == null || packageName.isEmpty()) return false;
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    /**
     * 获取应用版本号
     */
    public int getAppVersionCode(Context context, String packageName) {
        PackageInfo packageInfo = getPackageInfo(context, packageName);
        return packageInfo != null ? packageInfo.versionCode : -1;
    }
    /**
     * 获取应用版本名
     */
    public String getAppVersionName(Context context, String packageName) {
        PackageInfo packageInfo = getPackageInfo(context, packageName);
        return packageInfo != null ? packageInfo.versionName : null;
    }

    /**
     * 获取应用名称
     * @param context
     * @param packageName
     * @return
     */
    public String getAppName(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
            return pm.getApplicationLabel(appInfo).toString();
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }

    /**
     * 获取应用图标
     * @param context
     * @param packageName
     * @return
     */
    public Drawable getAppIcon(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
            return pm.getApplicationIcon(appInfo);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }

    /**
     * 解析未安装的 APK 文件
     * @param context
     * @param apkPath
     * @return
     */
    public PackageInfo getApkInfo(Context context, String apkPath) {
        PackageManager pm = context.getPackageManager();
        return pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
    }
}
